package com.arcvideo.acsconnect.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.arcvideo.acsconnect.app.UserDataCenter;
import com.arcvideo.rtcengine.helper.RtcEngineHelper;
import com.arcvideo.tyingyitonginhouse.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2017/7/28
 * @description
 */

public class EditAddressDialog extends Dialog {

    @BindView(R.id.et_address)
    EditText mEtAddress;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    private Context mContext;
    private AddressAdapter mAddressAdapter = new AddressAdapter();

    public EditAddressDialog(@NonNull Context context) {
        super(context, R.style.Dialog);
        this.mContext = context;
        init();
    }

    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_dialog_edit_address, null, false);
        ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        ButterKnife.bind(this, view);
        addContentView(view, layoutParams);

        Window window = this.getWindow();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = window.getAttributes();
        p.width = (int) (d.getWidth() * 0.97);
//        p.height = (int) (dFormat.getHeight() * 0.7);
        window.setAttributes(p);

        setAddress(UserDataCenter.getInstance().getImAddress());
        mRecyclerView.setAdapter(mAddressAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAddressAdapter.setData(buildList());
    }

    public void setAddress(String address) {
        if (!TextUtils.isEmpty(address)) {
            mEtAddress.setText(address);
        }
    }

    @OnClick({R.id.tv_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_confirm:
                String address = mEtAddress.getText().toString();
                if (!TextUtils.isEmpty(address)) {
                    UserDataCenter.getInstance().setImAddress(address);
                }
                dismiss();
                break;
        }
    }

    @Override
    public void show() {
        super.show();
//        showKeyboard(mEtAddress);
    }

    private List<AddressBean> buildList() {
        AddressBean test = new AddressBean(RtcEngineHelper.HOST_TEST);
        AddressBean dev = new AddressBean(RtcEngineHelper.HOST_DEV);
        AddressBean stg = new AddressBean(RtcEngineHelper.HOST_STG);
        AddressBean release = new AddressBean(RtcEngineHelper.HOST_RELEASE);
        AddressBean dev2 = new AddressBean(RtcEngineHelper.HOST_DEV2);
        List<AddressBean> list = new ArrayList<>();
        list.add(test);
        list.add(dev);
        list.add(stg);
        list.add(release);
        list.add(dev2);
        return list;
    }

    private void showKeyboard(final EditText editText) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(editText, 0);
            }
        }, 200);
    }

    class AddressBean {
        public AddressBean(String address) {
            this.address = address;
        }

        private String address;

        public String getAddress() {
            List<String> list = Arrays.asList();
            String[] args = (String[]) list.toArray();
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }
    }

    class AddressAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private List<AddressBean> mList = new ArrayList<>();
        private Context mContext;

        public void setData(List<AddressBean> list) {
            this.mList = list;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            mContext = parent.getContext();
            return new BodyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_address, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            final AddressBean addressBean = mList.get(position);
            BodyViewHolder body = (BodyViewHolder) holder;
            body.mTvAddress.setText(addressBean.getAddress());
            body.mTvAddress.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mEtAddress.setText(addressBean.getAddress());
                }
            });
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        class BodyViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.tv_address)
            TextView mTvAddress;

            public BodyViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
